<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'eleontev/Util/CryptEmail.php';
require_once APP_MODULE_DIR . 'knowledgebase/comment/inc/KBCommentView_helper.php';


class KBClientView_entry extends KBClientView_common
{

	function &execute(&$manager) {
		
		$row = &$manager->getEntryById($this->entry_id, $this->category_id);
		$row = &$this->stripVars($row);
		if(!$row) { $t = ''; return $t; }
		
		
		$this->meta_title = &$row['title'];
		$this->meta_keywords = &$row['meta_keywords'];
		$this->meta_description = &$row['meta_description'];
		
		$title = '';
		if($manager->getSetting('show_title_nav') && strlen($row['title']) < 80) {
			$title = $row['title'];
		}
		
		$data = $this->getTop($manager, $title);
		$data .= $this->getEntry($manager, $row);
		$data .= $this->getEntryListAttachment($manager);
		$data .= $this->getCommentList($manager);
		
		if($this->isCommentable($manager, $row['commentable'])) {
			if(!$manager->is_registered && $manager->getSetting('allow_comments') == 2) {
			
			// display form if at least one comment exists
			} elseif($manager->isComments($this->entry_id)) {
				$data .= $this->getCommentForm($manager);		
			}
		}
		
		$related = &$this->stripVars($manager->getEntriesRelated($this->entry_id));
		
		$data .= $this->getEntryListRelated($manager, $related);
		$data .= $this->getEntryListCategory($manager, $related);
		
		return $data;		
	}
	
	
	function getEntry(&$manager, &$row)  {
		
		$glossary_items = &$manager->getGlossaryItems();
		if($glossary_items) {
			$row['body'] = DocumentParser::parseGlossaryItems($row['body'], $glossary_items, $manager);
		}
		
		if(DocumentParser::isLink($row['body'])) {
			$row['body'] = DocumentParser::parseLink($row['body'], array($this, '_getLink'), $manager, $this->entry_id);	
		}
	
		if(DocumentParser::isCode($row['body'])) {
			$row['body'] = DocumentParser::parseCode($row['body'], $this->conf['client_home_path']);
		}
		
		
		$tpl = &new tplTemplatez($this->template_dir . 'article.html');
		
		$tpl->tplAssign('rating', $this->_getRating($row['rating']));
		$tpl->tplAssign('updated_date', $this->getFormatedDate($row['ts_updated']));
		$tpl->tplAssign('article_block', $this->getEntryBlock($row, $manager));
		$tpl->tplAssign('base_href', $this->conf['client_home_path']);
		
		$tpl->tplParse(array_merge($row, $this->msg));
		return $tpl->tplPrint(1);
	}
	
	
	function getEntryBlock($data, $manager) { 
		
		$tpl = &new tplTemplatez($this->template_dir . 'article_block.html');
		
		$tpl->tplAssign('article_id', $data['id']);
		$tpl->tplAssign('category_id', $data['category_id']);	
		$tpl->tplAssign('print_link', $this->_getLink('print', $data['category_id'], $data['id']));
		
		
		// authors,  dates
		if($manager->getSetting('show_author')) {
			
			$tpl->tplAssign('date_posted_formated', $this->getFormatedDate($data['ts_posted']));
			$tpl->tplAssign('date_updated_formated', $this->getFormatedDate($data['ts_updated']));
			
			require_once 'eleontev/Item/PersonHelper.php';
			$user = $manager->getAdminInfo($data['author_id']);
			$tpl->tplAssign('author', PersonHelper::getShortName($user));
			
			$user = $manager->getAdminInfo($data['updater_id']);
			$tpl->tplAssign('updater', PersonHelper::getShortName($user));
			
			$tpl->tplSetNeeded('/author_block');
		}
		
		
		if($manager->getSetting('show_hits')) {
			$tpl->tplSetNeededGlobal('show_hits');
		}
		
		if($manager->getSetting('show_send_link')) {
			$tpl->tplSetNeededGlobal('send_link');
			$tpl->tplAssign('send_link', $this->_getLink('send', $data['category_id'], $data['id']));		
		}		
		
		if($this->isCommentable($manager, $data['commentable'])) {
			$tpl->tplSetNeededGlobal('commentable');
			$tpl->tplAssign('comment_link', $this->_getLink('comment', $data['category_id'], $data['id']));		
		}
		
		
		if($this->isRatingable($manager, $data['ratingable'])) {
			
			$tpl->tplSetNeededGlobal('ratingable');
			
			if($manager->isUserVoted($data['id']) === false) {
				$select = new FormSelect();
				$select->select_tag = false;
				$select->setRange($this->_getRateRange(), array(0=>$this->msg['add_rate_msg']));
				$tpl->tplAssign('rating_select', $select->select());
				$tpl->tplSetNeeded('/vote_form');
			
			} else {
			
				$tpl->tplAssign('rating', $this->_getRating($data['rating']));
				$tpl->tplSetNeeded('/show_rating');
			}
			
			
			$tpl->tplAssign('form_action', $this->_getLink('entry', $data['category_id'], $data['id']));
			$tpl->tplAssign('votes', ($data['votes']) ? $data['votes'] : 0);		
		}		
		
		
		$tpl->tplParse(array_merge($data, $this->msg));
		return $tpl->tplPrint(1);
	}
	
	
	function getEntryListAttachment(&$manager) {
		
		$rows = $manager->getAttachmentList($this->entry_id);
		if(!$rows) { return; }

		$tpl = &new tplTemplatez($this->template_dir . 'article_list_attachment.html');
			
		foreach($rows as $file_id => $v) {
			
			// need to fix when file private
			//$private = ($v['private'] || $v['category_private']);
			//$v['item_img'] = $this->_getItemImg($manager->is_registered, $private, true);
			
			$ext = substr($v['filename'], strrpos($v['filename'], ".")+1);
			$v['item_img'] = $this->_getItemImg($manager->is_registered, false, 'file', $ext);

			$v['filesize'] = WebUtil::getFileSize($v['filesize']);
			$v['attachment_link'] = $this->_getLink('file', $this->entry_id, $file_id);
			$v['attachment_title'] = ($v['title']) ? $v['title'] : $v['filename'];
			$v['attachment_title'] = $this->stripVars($v['attachment_title']);
			$tpl->tplParse($v, 'row');
		}
		
		$tpl->tplAssign('title_msg', $this->msg['attachment_title_msg']);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}	
	
	
	function getEntryListCategory(&$manager, $have_related) {
		
		$limit_num = $manager->getSetting('num_entries_category');
		$rows = &$this->stripVars($manager->getEntriesCategory($this->category_id, $this->entry_id, $limit_num));
		$rows_num = count($rows);
		if(!$rows) { return; }
		
		
		$tpl = &new tplTemplatez($this->template_dir . 'article_list_category.html');
		
		if($limit_num != 'all' && ($rows_num > $limit_num)) {
		
			shuffle($rows);
			$rows = &array_slice($rows, 0, $limit_num);
		
			$tpl->tplAssign('category_link', $this->_getLink('index', $this->category_id));
			$tpl->tplAssign('category_link_msg', $this->msg['more_entries_msg']);
			$tpl->tplSetNeeded('/category_link');
		}		

		foreach($rows as $k => $v) {
			$v['item_img'] = $this->_getItemImg($manager->is_registered, $v['private']);
			$v['entry_link'] = $this->_getLink('entry', $this->category_id, $v['entry_id']);
			$v['title'] = $this->stripVars($v['title']);
			
			$tpl->tplParse($v, 'row');
		}
		
		$tpl->tplAssign('title_msg', $this->msg['entry_category_title_msg']);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function getEntryListRelated(&$manager, $rows) {
		
		if(!$rows) { return; }
		
		$tpl = &new tplTemplatez($this->template_dir . 'article_list_related.html');
		
		foreach($rows as $k => $v) {
		
			$private = ($v['private'] || $v['category_private']);
			$v['item_img'] = $this->_getItemImg($manager->is_registered, $private);
			$v['entry_link'] = $this->_getLink('entry', $v['category_id'], $v['entry_id']);
			$v['title'] = $this->stripVars($v['title']);
			
			$tpl->tplParse($v, 'row');
		}
		
		$tpl->tplAssign('title_msg', $this->msg['entry_related_title_msg']);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}		
	
	
	function getCommentList(&$manager) {
		
		$tpl = &new tplTemplatez($this->template_dir . 'comment_list.html');
		
		$bp = $this->pageByPage(20, $manager->getCommentListCountSql($this->entry_id));
		$by_page = $bp->shortNavigate();
		$tpl->tplAssign('page_by_page', $by_page);
		
		if($bp->num_pages > 1) {
			$tpl->tplSetNeeded('/by_page_bottom');
		}
		
		$rows = &$this->stripVars($manager->getCommentList($this->entry_id, $bp->limit, $bp->offset));
		if(!$rows) { return; }
		
		
		$parser = &KBCommentView_helper::getBBCodeObj();
		
		foreach($rows as $k => $row) {
			
			if($row['email']) {
				$tpl->tplSetNeeded('row/if_email');
				$tpl->tplAssign('mailto_link', CryptEmail::getMailto($row['email']));
			} elseif($row['name']) {
				$tpl->tplSetNeeded('row/if_name');
			}
			
			$tpl->tplAssign('comment', $parser->qparse(nl2br($row['comment'])));		
			$tpl->tplAssign('formated_date', $this->getFormatedDate($row['ts']));
			$tpl->tplParse($row, 'row');
		}
		
		$tpl->tplAssign('kb_path', $this->controller->kb_path);
		$tpl->tplAssign('var', 'value');
		$tpl->tplAssign('js_script', CryptEmail::getMailtolJs(false));
		$tpl->tplAssign($this->msg);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}	
	
	
	function getCommentForm($manager) {
		
		$tpl = &new tplTemplatez($this->template_dir . 'comment_form.html');
		
		$tpl->tplAssign('action_link', $this->_getLink('comment', $this->category_id, $this->entry_id));
		
		if(!$manager->is_registered) {
			$tpl->tplSetNeeded('/not_registered');
		}
		
		if($this->useCaptcha($manager, 'comment')) {
			$tpl->tplSetNeeded('/captcha');
			$tpl->tplAssign('captcha_src', $this->getCaptchaSrc());
		}		
		
		
		$tpl->tplAssign('toggle_form', 1);
		$tpl->tplAssign('display_form', 'none');		
		
		$tpl->tplAssign('kb_path', $this->controller->kb_path);
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($this->getFormData());	
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
}
?>